package com.example.book.web;

import com.example.book.pojo.Book;
import com.example.book.pojo.Page;
import com.example.book.service.BookService;
import com.example.book.service.impl.BookServiceImpl;
import com.example.book.utils.WebUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "BookServlet", value = "/manager/bookServlet")
public class BookServlet extends BaseServlet {
    private BookService bookService = new BookServiceImpl();

    /**
     * 处理分页功能
     * @param request
     * @param response
     * @throws IOException
     */
    protected void page(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
        //1.获取请求参数pageNo 和pageSize
        int pageNo = WebUtils.parseInt(request.getParameter("pageNo"),1);
        int pageSize = WebUtils.parseInt(request.getParameter("pageSize"), Page.PAGE_SIZE);
        //2.调用BookService.page(pageNo,pageSize):page对象
        Page<Book> page = bookService.page(pageNo,pageSize);

        page.setUrl("manager/bookServlet?action=page");

        //3.保存Page对象到Request域中
        request.setAttribute("page",page);
        //4.请求转发到pages/manager/book_manager.jsp页面
        request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request,response);
    }

    protected void add(HttpServletRequest request,HttpServletResponse response) throws IOException {
        int pageNo = WebUtils.parseInt(request.getParameter("pageNo"),0);
        pageNo+=1;
        //1.获取请求的参数，封装成为Book对象
        Book book = WebUtils.copyParamToBean(request.getParameterMap(), new Book());
        //2.调用BookService.addBook()保存图书
        bookService.addBook(book);
        //3.跳到图书列表页面（/manager/bookService/?action=list）,使用重定向进行跳转，请求转发地址栏不变，刷新页面，会重复添加数据
        response.sendRedirect(request.getContextPath()+"/manager/bookServlet?action=page&pageNo=" + pageNo);
    }
    protected void delete(HttpServletRequest request,HttpServletResponse response) throws IOException {
        //1.获取请求的参数id，图书编号
        int id = WebUtils.parseInt(request.getParameter("id"),0);
        //2.调用bookService.deleteBookById();删除图书
        bookService.deleteBookById(id);
        //3.重定向回图书列表管理页面（/book/manager/bookServlet?action=list）


        response.sendRedirect(request.getContextPath()+"/manager/bookServlet?action=page&pageNo=" + request.getParameter("pageNo"));

    }
    protected void update(HttpServletRequest request,HttpServletResponse response) throws IOException {

        Book book = WebUtils.copyParamToBean(request.getParameterMap(), new Book());
        bookService.updateBook(book);
        String pageNo = request.getParameter("pageNo");
        response.sendRedirect(request.getContextPath()+"/manager/bookServlet?action=page&pageNo=" + request.getParameter("pageNo"));
    }

    protected void getBook(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        int id = WebUtils.parseInt(request.getParameter("id"), 0);
        Book book = bookService.queryBookById(id);
        request.setAttribute("book",book);
        request.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(request,response);
    }

    protected void list(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //1.通过BookService查询全部图书
        List<Book> books = bookService.queryBook();
        //2.把全部图书保存到request域中
        request.setAttribute("books",books);
        //3.请求转发到/pages/manager/book_manager.jsp页面
        request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request,response);
    }


}
